import random      #随机模块，代码为灰色，本模块实际上未被引用
import time        #时间模块
import os          #操作系统接口模块

import os,django   #操作系统接口模块，开发web的框架
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "phoneShop.settings") # project_name 项目名称，开发环境配置文件
django.setup()     #django 实例化

import DataProcess.DataCleaning     #数据清洗
from Create_address.AddressProcess import start_process, joint_address, return_address
from app import models
from app.models import ShopTable, MerchTable, UserTable, admin

def return_df():#把excel文件里面的数据传入到一个列表
    counter = 0
    return_list = []
    result_df = DataProcess.DataCleaning.data_process_return(r'C:\Users\Hansel\Desktop\作业\大三下\workshop\电商网站\phoneShop')
    df = result_df.drop_duplicates(["s_id"])  # drop the repeat value
    df2 = start_process()  # start edit process
    se1 = joint_address(df2)
    for row in df.iterrows():
        #print(str(list(row[1])[-1])[:-2])
        row_list = []
        row_list.append(str(list(row[1])[-1])[:-2])
        row_list.append(list(row[1])[-4])
        se2 = 'nan'
        while(len(str(se2)) <= 4):
            se2 = return_address(se1)
        row_list.append(se2)

        return_list.append(row_list)

        counter += 1
        # if counter % 100 == 0:  # view the insert process
        #     print(counter)

    return return_list#传回一个双层列表

def save_shoplist_to_txt(shop_list):#把双层列表保存到txt
    shop_str = []
    for i in shop_list:
        #print(i)
        list_to_str = str(i)
        shop_str.append(list_to_str)
    #print(shop_str[:10])

    f = open("shop_list.txt", "w", encoding='utf-8')
    n_str = '\n'
    #print(str)
    f.write(n_str.join(shop_str))
    f.close()


def read_shop_txt(): #读取txt中的数据
    # str1 = os.getcwd()
    # print(str1)
    # str2 = '\shop_list.txt'
    # str3 = str1 + str2
    # print(str3)
    file_type = 'shop_list'
    paths = os.walk(os.getcwd())
    for root, dirs, files in paths:
        for file in files:
            if file_type in file:
                # print(12354)
                #print(file)
                str3 = os.path.join(root,file)#带路径的文件名
    print(str3)
    s = []
    f = open(str3,'r', encoding='utf-8') #由于我使用的pycharm已经设置完了路径，因此我直接写了文件名
    for lines in f:
    #query_list.append(line.replace('/','').replace('、','').replace(' ','').strip(' '))
        ls = lines.strip(' ').replace(' ','').replace('、','/').replace('?','').split('/')
        for i in ls:
            s.append(i)
    f.close()
    shop_list = []
    counter = 0
    for i in s:
        #print(i)
        #print(type(i))
        list_line = eval(i)
        # print(list_line)
        # print(type(list_line))
        # print(list_line[1])
        #print(counter)
        shop_list.append(list_line)
        counter += 1
    # print(123)
    # print(shop_list[-1][1])
    # print(234)
    return shop_list


def insert_shop_bylist(shop_list):#insert the edited shop data into schema
    counter = 0

    for i in shop_list:
        #print(i)
        s = ShopTable()
        s.Shop_id = i[0]
        s.Shop_name = i[1]
        s.Shop_address = i[2]
        # print(s.Shop_id)
        # print(s.Shop_name)
        # print(s.Shop_address)
        s.save()

        counter += 1

        # if counter >= 10:
        #     break

        if counter % 100 == 0:  # view the insert process
            print(counter)

def return_merch_list():
    counter = 0
    result_df = DataProcess.DataCleaning.data_process_return(r'C:\Users\Hansel\Desktop\作业\大三下\workshop\电商网站\phoneShop')
    # print(result_df)
    return_list = []
    for row in result_df.iterrows():
        # if counter >= 10:
        #     break
        if counter % 100 == 0:
            print(counter)

        data_list = list(row[1])
        # print(data_list)

        merch = []
        merch.append(str(data_list[-2]))
        merch.append(data_list[3])
        merch.append(data_list[0])
        merch.append(data_list[2].split("\\")[-1][2:])
        merch.append(data_list[1])
        if(ShopTable.objects.get(Shop_id=str(data_list[-1])[:-2])):
            merch.append(str(ShopTable.objects.get(Shop_id=str(data_list[-1])[:-2]).Shop_id))
            return_list.append(merch)
            counter += 1
        else:
            continue
    #print(return_list)
    print(counter)
    return return_list

def save_merchlist_to_txt(merch_list):#把双层列表保存到txt
    merch_str = []
    for i in merch_list:
        #print(i)
        list_to_str = str(i)
        merch_str.append(list_to_str)
    #print(merch_str[:10])

    f = open("merch_list.txt", "w", encoding='utf-8')
    n_str = '\n'
    #print(str)
    f.write(n_str.join(merch_str))
    f.close()

def read_merch_txt():
    file_type = 'merch_list'
    paths = os.walk(os.getcwd())
    for root, dirs, files in paths:
        for file in files:
            if file_type in file:
                # print(12354)
                # print(file)
                str3 = os.path.join(root, file)  # 带路径的文件名
    print(str3)
    merch_list = []
    f = open(str3, 'r', encoding='utf-8')  # 由于我使用的pycharm已经设置完了路径，因此我直接写了文件名
    for lines in f:
        #print(lines)
        list_line = eval(lines)
        merch_list.append(list_line)
    f.close()

    return merch_list

def insert_merch_bylist(merch_list):#insert the edited shop data into schema
    counter = 0

    for i in merch_list:
        m = MerchTable()
        m.Merch_id = i[0]
        m.Merch_name = i[1]
        m.Merch_img = i[2]
        m.Merch_tag = i[3]
        m.Merch_price = str(i[4])
        if (models.ShopTable.objects.filter(Shop_id = str(i[5]))):
            m.Merch_sid = models.ShopTable.objects.get(Shop_id = str(i[5]))
            # print(m.Merch_id)
            # print(m.Merch_name)
            # print(m.Merch_img)
            # print(m.Merch_tag)
            # print(m.Merch_price)
            #print(m.Merch_sid)
            m.save

            counter += 1

        # if counter % 100 == 0:  # view the insert process
        #     print(counter)



if __name__ == '__main__':
    print("Start print the shop list")
    # #shop_list = return_df()
    # # print(shop_list[:10])
    # #save_shoplist_to_txt(shop_list)
    # shop_list = read_shop_txt()
    # print(shop_list[:10])
    # #insert_shop_bylist(shop_list)
    # print("Done")

    #return_list = return_merch_list()
    # for i in return_list:
    #     print(i)
    #save_merchlist_to_txt(return_list)
    merch_list = read_merch_txt()
    # counter = 0
    # for i in merch_list:
    #     counter +=1
    # print(counter)
    #print(merch_list)
    insert_merch_bylist(merch_list)

